<?php


namespace serv\queue\warehouse;

use L;
use of;
use Matrix\Exception;
use common\batchInsert;
use Sdk\Fop\CallFopApiUtil;
use Sdk\Winit\CallApiUtil;

class InboundOrder
{
    public static function inbound($arr)
    {
        $data = $arr['data'];
        try {
            $service = ucfirst($data['service']);
            if (!empty($service)) {
                if (method_exists(__CLASS__, $service)) {
                    $result = self::$service($data);
                    if (!empty($result)) {
                        of::work(['warehouse']);
                        //更新调拨详情数据
                        $total_received = 0;
                        if (!empty($result['skus'])) {
                            foreach ($result['skus'] as $sku) {
                                $sql = "SELECT
                                            `detail_id`,
                                            `qty` 
                                        FROM
                                            `transfer_order_detail` 
                                        WHERE
                                            `transfer_order_id` = '{$data['transfer_order_id']}' 
                                            AND `sku` = '{$sku['sku']}'";
                                $row = L::sql($sql, 'warehouse');
                                if (!empty($row)) {
                                    if (!empty($row)) {
                                        $details = [
                                            'qty_received' => $sku['qty_received'],
                                            'manual_qty' => $sku['qty_received'],
                                            'diff_qty' => $sku['qty_received'] > 0 ? $sku['qty_received'] - $row[0]['qty'] : 0
                                        ];
                                        $total_received += $sku['qty_received'];
                                        $sku_where = "detail_id = '{$row[0]['detail_id']}'";
                                        $detail_sql = batchInsert::update($details, 'transfer_order_detail', $sku_where);
                                        L::sql($detail_sql, 'warehouse');
                                    }
                                }
                            }
                            //更新调拨列表信息
                            $items = [
                                'total_received' => $total_received,
                                'total_manual' => $total_received,
                                'diff_qty' => $total_received > 0 ? $total_received - $data['total_qty'] : 0,
                                'inbound_data' => json_encode($result['inbound_data'], JSON_UNESCAPED_UNICODE),
                            ];
                            $where = "transfer_order_id = '{$data['transfer_order_id']}'";
                            $sql = batchInsert::update($items, 'transfer_order', $where);
                            L::sql($sql, 'warehouse');
                        }
                        of::work(true);
                    }
                }
            }
            return true;
        } catch (Exception $e) {
            of::work($e);
            return false;
        }
    }

    // 获取递四方入库详情 4px 更新入库状态和上架数量
    public static function Fop($params)
    {
        set_time_limit(0);
        $method = 'fu.wms.inbound.getlist';
        $data = [
            'consignment_no' => $params['platform_no']
        ];
        $result = CallFopApiUtil::callFopApi($method, $data);
        $items = $result['data']['data'][0] ?? [];
        if (!empty($items)) {
            $status = self::InboundStatus($params['service']);
            $inbound_status = 'F_'.$items['status'];
            $inbound_status_name = $status[$items['status']] ?? '';
            $lstsku = $items['lstsku'] ?: [];
            $skus = [];
            if (!empty($lstsku)) {
                foreach ($lstsku as $val) {
                    if (isset($skus[$val['sku_code']])) {
                        $skus[$val['sku_code']]['qty_received'] += $val['putaway_qty'];
                    } else {
                        $skus[$val['sku_code']] = [
                            'qty_received' => $val['putaway_qty'],
                            'sku' => $val['sku_code']
                        ];
                    }
                }
            }
            return [
                'skus' => $skus,
                'inbound_data' => $items,
                'status' => $inbound_status,
                'status_name' => $inbound_status_name
            ];
        }
        return [];
    }

    // 万邑通
    public static function Winit($params)
    {
        $method = 'winit.wh.inbound.getOrderDetail';
        $data = [
            'orderNo' => $params['platform_no'],
            'isIncludePackage' => 'Y',
        ];
        $result = CallApiUtil::callWinitApi($method, $data);
        $items = $result['data']?? [];
        if (!empty($items)) {
            $status = self::InboundStatus($params['service']);
            $inbound_status = 'W_'.$items['status'];
            $inbound_status_name = $status[$items['status']] ?? '';
            $packageList = $items['packageList'] ?: [];
            $skus = [];
            $details = [];
            if (!empty($packageList)) {
                foreach ($packageList as $list) {
                    foreach ($list['merchandiseList'] as $val) {
                        $details[] = [
                            'thirdPartyCaseNo' => $list['thirdPartyCaseNo'],
                            'sellerWeight' => $list['sellerWeight'],
                            'length' => $list['length'],
                            'weight' => $list['weight'],
                            'sellerWidth' => $list['sellerWidth'],
                            'actualTrackingNo' => $list['actualTrackingNo'],
                            'packageNo' => $list['packageNo'],
                            'sellerCaseNo' => $list['sellerCaseNo'],
                            'inspectionQty' => $val['inspectionQty'],
                            'quantity' => $val['quantity'],
                            'standardPartsNum' => $val['standardPartsNum'],
                            'actualQuantity' => $val['actualQuantity'],
                            'adjustStorgeStatus' => $val['adjustStorgeStatus'],
                            'specification' => $val['specification'],
                            'sku' => $val['sku'],
                            'skuType' => $val['skuType'],
                            'merchandiseCode' => $val['merchandiseCode'],
                            'shelvesTime' => $list['shelvesTime'],
                            'unloadingTime' => $list['unloadingTime'],
                            'sellerLength' => $list['sellerLength'],
                            'width' => $list['width'],
                            'isAutoBoxStorage' => $list['isAutoBoxStorage'],
                            'sellerHeight' => $list['sellerHeight'],
                            'height' => $list['height'],
                            'status' => $list['status'],
                        ];
                        if (isset($skus[$val['merchandiseCode']])) {
                            $skus[$val['merchandiseCode']]['qty_received'] += $val['actualQuantity'];
                        } else {
                            $skus[$val['merchandiseCode']] = [
                                'qty_received' => $val['actualQuantity'],
                                'sku' => $val['merchandiseCode']
                            ];
                        }
                    }
                }
                $items['items'] = $details;
            }
            return [
                'skus' => $skus,
                'inbound_data' => $items,
                'status' => $inbound_status,
                'status_name' => $inbound_status_name
            ];
        }
        return [];
    }

    //转换各渠道状态
    public static function InboundStatus($service)
    {
        $arr = strtoupper($service);
        return of::config('env.'.$arr.'_INBOUND_STATUS');
    }
}
